/**
 * GWT Kad : Kademlia implementation for GWT
 * Copyright (C) 2012  Pierrick HYMBERT, Paul-André MISTRAL, Florian ALEXANDRE
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.kad.gwt.web.client.kademelia.model.rpc.store;

import org.kad.gwt.web.client.kademelia.model.KadId;
import org.kad.gwt.web.client.kademelia.model.KadNode;
import org.kad.gwt.web.client.kademelia.model.rpc.KadRpcMesageType;
import org.kad.gwt.web.client.kademelia.model.rpc.KadRpcRequest;

/**
 * A store request.
 * 
 * @author Pierrick HYMBERT &lt;pierrick [dot] hymbert [at] gmail [dot] com&gt;
 */
public class KadRpcStoreRequest extends KadRpcRequest {

    /**
     * serial uid
     */
    private static final long serialVersionUID = 8444252426415225048L;
    /**
     * The associated data.
     */
    private  KadId            key;
    /**
     * The associated data.
     */
    private  String            data;

    /**
     * For serialization purpose ...
     */
    KadRpcStoreRequest() {
    }

    /**
     * Constructor.
     * 
     * @param sourceIn
     *            The source of the ping request
     * @param targetIn
     *            The target of the ping request
     * @param dataIn
     *            The data to store
     */
    public KadRpcStoreRequest(final KadNode sourceIn, final KadNode targetIn, String dataIn) {
        this(sourceIn, targetIn, computeKey(dataIn), dataIn);
    }

    /**
     * Constructor.
     * 
     * @param sourceIn
     *            The source of the ping request
     * @param targetIn
     *            The target of the ping request
     * @param keyIn
     *            The key to store
     * @param dataIn
     *            The data to store
     */
    public KadRpcStoreRequest(final KadNode sourceIn, final KadNode targetIn, KadId keyIn, String dataIn) {
        super(sourceIn, targetIn, KadRpcMesageType.STORE);
        this.data = dataIn;
        this.key = keyIn;
    }

    /**
     * Compute the key corresponding to this data.
     *
     * @param data
     * @return
     */
    private static KadId computeKey(String dataToCompute) {
        return new KadId(String.format("%x", dataToCompute)); //FIXME Use a real hash (SHA ?)
    }

    /**
     * @return {@link #data}
     */
    public String getData() {
        return data;
    }
    
    /**
     * @return {@link #key}
     */
    public KadId getKey() {
        return key;
    }
}
